from fastapi import FastAPI
from src.utils.redis import RedisHelper
from src.utils.logger import get_logger
logger = get_logger("capcut")

def init_app_events(app: FastAPI):
    """注册应用事件处理器"""
    
    @app.on_event("startup")
    async def startup_event():
        """应用启动时执行"""
        try:
            # 初始化Redis
            RedisHelper.initialize()
            logger.info("Redis全局实例初始化成功")

        except Exception as e:
            logger.error(f"应用启动初始化失败: {str(e)}")
            raise
    
    # @app.on_event("shutdown")
    # async def shutdown_event():
    #     """应用关闭时执行"""
    #     try:
    #         redis = RedisHelper.get_instance()
    #         redis.close()  # 使用新的close方法
    #         logger.info("Redis连接池已关闭")
    #     except Exception as e:
    #         logger.error(f"Redis关闭失败: {str(e)}")